unit uObjectOptions;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls,
  uObject, ExtDlgs;

type
  TGameObjectOptions = class(TForm)
    ActionLabel: TLabel;
    OKButton: TButton;
    CancelButton: TButton;
    ObjectType: TComboBox;
    ObjectImage: TImage;
    TileSetter: TEdit;
    TexPosX: TEdit;
    TexPosY: TEdit;
    PictureLoadButton: TButton;
    OpenDialog: TOpenPictureDialog;
    ActionList: TListBox;
    SpecialEdit1: TEdit;
    SpecialEdit2: TEdit;
    // )
    procedure onCreate(Sender: TObject);
    procedure onObjectClick(Sender: TObject);
    procedure FormCanResize(Sender: TObject; var NewWidth, NewHeight: Integer;
      var Resize: Boolean);
    procedure FormShow(Sender: TObject);
    procedure ObjectTypeChange(Sender: TObject);
    procedure ActionListDblClick(Sender: TObject);
    procedure SpecialEdit1Change(Sender: TObject);
    procedure SpecialEdit2Change(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    obj: TGameObject;
    x, y: integer;
  end;

var
  GameObjectOptions: TGameObjectOptions;

implementation

{$R *.dfm}

uses
  uMapEditor, uTileSet, uAction, uActionOptions;

procedure TGameObjectOptions.ActionListDblClick(Sender: TObject);
var
  i: integer;
begin
  if (Sender is TListBox) then
    begin
      if (((Sender as TListBox) = self.ActionList) and
         (self.ActionList.Items.Count > 0) and
         (self.ActionList.ItemIndex >= 0)) then
        begin
          if (ActionList.Items.Strings[ActionList.ItemIndex] = '...') then
            begin
              for i := low(obj.actions) to high(obj.actions) do
                begin
                  if (obj.actions[i].actionType = 'not') then
                    begin
                      exit;
                    end;
                end;
              SetLength(obj.actions, length(obj.actions)+1);
              obj.actions[high(obj.actions)] := TGameAction.Create();
              obj.actions[high(obj.actions)].actionType := 'not';
              obj.actions[high(obj.actions)].arguments := '';
              GameActionMenu.act := obj.actions[high(obj.actions)];
              GameActionMenu.actI := high(obj.actions);
              GameActionMenu.Show;
              self.Show;
            end
          else
            begin
              for i := low(obj.actions) to high(obj.actions) do
                begin
                  if (ActionList.Items.Strings[ActionList.ItemIndex] = obj.actions[i].actionType) then
                    begin
                      GameActionMenu.act := obj.actions[i];
                      GameActionMenu.actI := i;
                      GameActionMenu.Show;
                    end;
                end;
            end;
        end;
    end;
end;

procedure TGameObjectOptions.FormCanResize(Sender: TObject; var NewWidth,
  NewHeight: Integer; var Resize: Boolean);
begin
  Resize := false;
end;

procedure TGameObjectOptions.FormShow(Sender: TObject);
var
  x, y: integer;
begin
  self.ActionList.Items.Clear;
  for x := low(obj.actions) to high(obj.actions) do
    begin
      self.ActionList.Items.Add(obj.actions[x].actionType);
    end;
  self.ActionList.Items.Add('...');
  self.ObjectType.Items.Clear;
  self.ObjectType.Text := obj.objectType;
  self.ObjectType.Items.Add('not');
  self.ObjectType.Items.Add('invisible');
  self.ObjectType.Items.Add('chest');
  self.ObjectType.Items.Add('chest_key');
  self.ObjectType.Items.Add('npc');
  self.ObjectType.Items.Add('sign');
  self.ObjectType.Items.Add('grass');
  self.ObjectType.Items.Add('key');
  self.ObjectType.Items.Add('fiend');
  self.ObjectType.Items.Add('door');
  self.ObjectType.Items.Add('shop');
  self.TileSetter.Text := 'failed Texture';
  y := 0;
  x := obj.texturePosition;
  if (obj.texture <> nil) then
    begin
      self.TileSetter.Text := obj.texture.name;
      while (x > round(obj.texture.Width/TileInPixel)) do
        begin
          x := x - round(obj.texture.Height/TileInPixel);
          y := y + 1;
        end;
    end;
  self.TexPosX.Text := IntToStr(x);
  self.TexPosY.Text := IntToStr(y);
  self.SpecialEdit1.Text := obj.text1;
  self.SpecialEdit2.Text := obj.text2;
  self.ObjectImage.Canvas.Draw(round(self.ObjectImage.Width/4), round(self.ObjectImage.Height/4), obj.texture.getTextureFrom(obj.texturePosition));
end;

procedure TGameObjectOptions.ObjectTypeChange(Sender: TObject);
begin
  obj.objectType := self.ObjectType.Text;
  if ((obj.objectType <> 'not') and (obj.objectType <> 'invisible')) then
    begin
      self.SpecialEdit1.Enabled := true;
      self.SpecialEdit1.Text := '...';
    end
  else
    begin
      self.SpecialEdit1.Enabled := false;
      self.SpecialEdit1.Text := 'no';
    end;
  
  if ((obj.objectType = 'chest_key') or
     (obj.objectType = 'npc')) then
    begin
      self.SpecialEdit2.Enabled := true;
      self.SpecialEdit2.Text := '...';
    end
  else
    begin
      self.SpecialEdit2.Enabled := false;
      self.SpecialEdit2.Text := 'no';
      if (obj.objectType = 'fiend') then
        begin
          self.SpecialEdit2.Text := '0|0|-1|0|';
          obj.text2 := self.SpecialEdit2.Text;
        end;
    end;
end;

procedure TGameObjectOptions.onCreate(Sender: TObject);
begin
  self.DoubleBuffered := true;
  self.OKButton.OnClick := self.onObjectClick;
  self.CancelButton.OnClick := self.onObjectClick;
  self.PictureLoadButton.OnClick := self.onObjectClick;
end;

procedure TGameObjectOptions.onObjectClick(Sender: TObject);
var
  s: String;
  i: Integer;
  dot: Boolean;
begin
  if (Sender is TButton) then
    begin
      if ((Sender as TButton).Caption = self.OKButton.Caption) then
        begin
          x := x-1;
          GameMapEditor.screen.useMap.rooms[GameMapEditor.roomOfMap].objects[x, y].actions := obj.actions;
          GameMapEditor.screen.useMap.rooms[GameMapEditor.roomOfMap].objects[x, y] := obj;
          self.Hide();
          GameMapEditor.scrollMap(offsetX, offsetY);
          GameMapEditor.doubleClick := false;
        end
      else if ((Sender as TButton).Caption = self.CancelButton.Caption) then
        begin
          self.Hide();
          GameMapEditor.doubleClick := false;
        end
      else if (((Sender as TButton).Caption = self.PictureLoadButton.Caption) and
              (self.TileSetter.Text <> 'failed Texture')) then
        begin
          if (OpenDialog.Execute()) then
            begin
              ShowMessage(OpenDialog.FileName);
              if (FileExists(OpenDialog.FileName)) then
                begin
                  s := '';
                  dot := false;
                  for i := length(OpenDialog.FileName) downto 1 do
                    begin
                      if (OpenDialog.FileName[i] = '.') then dot := true
                      else if (OpenDialog.FileName[i] = '\') then dot := false
                      else if (dot) then
                        begin
                          s := OpenDialog.FileName[i] + s;
                        end;
                    end;
                  self.TileSetter.Text := s;
                  self.TexPosX.Text := '0';
                  self.TexPosY.Text := '0';
                  for i := low(GameMapEditor.screen.useMap.tileSets) to high(GameMapEditor.screen.useMap.tileSets) do
                    begin
                      if (GameMapEditor.screen.useMap.tileSets[i].name = s) then
                        begin
                          obj.texture := GameMapEditor.screen.useMap.tileSets[i];
                        end;
                    end;
                  obj.texturePosition := 0;
                  self.ObjectImage.Canvas.Draw(round(self.ObjectImage.Width/4), round(self.ObjectImage.Height/4), obj.texture.getTextureFrom(obj.texturePosition));
                end;
            end;
        end;
    end;
end;

procedure TGameObjectOptions.SpecialEdit1Change(Sender: TObject);
begin
  obj.text1 := self.SpecialEdit1.Text;
end;

procedure TGameObjectOptions.SpecialEdit2Change(Sender: TObject);
begin
  obj.text2 := self.SpecialEdit2.Text;
end;

end.
